<?php

namespace app\member\controller;

/** 控制器类引入区 **/

use app\common\controller\Smscm;

/** 模型类引入区 **/

use app\common\model\Order;
use app\common\model\Orderproject;
use app\common\model\User as UserM;
use app\common\model\ViperUser as ViperUserM;
use app\common\model\UsersYelp as UsersYelpM;

/** 核心或第三方类引入区 **/

use app\common\model\VipOrder as VipOrderM;
use app\web\constant\CommonConstant;
use my\Wx;
use think\Db;
use think\Validate;

/**
 *  用户 控制器
 * @package app\api\controller
 */
class Users extends Base
{
    public $noNeedLogin = [''];

    /**
     *  用户信息
     **/
    public function userInfo()
    {
        $user_id = $this->user_id;

        $user_info = $this->user_info;

        $info = ViperUserM::where('id', $user_id)->field('id,nickname,avatar,phone,ivt_code')->find();
        if ($info) {
            $info['vip_num'] = Db::name('qt_viper_card')
                ->field('vc.id,vc.viper_type_id,vc.avatar,vc.mobile,
                vc.birthday,vc.card_no,vc.amount,vt.name as type_name,vc.name,vc.create_time,
                vc.gender,vc.score,vc.amount,vc.status,vc.give_amount,s.name as store_name')
                ->alias('vc')
                ->leftJoin('qt_viper_type vt', 'vc.viper_type_id = vt.id')
                ->leftJoin('qt_store s', 'vc.store_id = s.id')
                ->where('vc.store_id', '=', $user_info['store_id'])
                ->where('vc.viper_user_id', '=', $user_info['id'])
                ->count(); //会员卡数量

            $info['coupon_num'] = Db::name('qt_ticket_send')
                ->alias('ts')
                ->field('ts.*,tk.id as tk_id ,tk.type,tk.name,tk.quota')
                ->leftJoin('qt_ticket tk', 'tk.id = ts.ticket_id')
                //->where("ts.is_delete", CommonConstant::IS_DELETE_NO)
                ->where("tk.store_id", $user_info['store_id'])
                ->where("ts.phone", $user_info['phone'])
                ->count(); //优惠券数量
            $info['room_money'] = Db::name('qt_order_project')->alias('p')
                ->field([
                    'p.price',
                ])
                ->leftJoin('qt_users u', 'p.engineer_id=u.id')
                ->leftJoin('qt_order qo', 'p.order_id=qo.id')
                ->leftJoin('qt_room m', 'p.room_id=m.id')
                ->where('qo.uv_id', $user_id)
                ->where('qo.store_id', $user_info['store_id'])
                ->where('p.is_delete',2)
                //->where('qo.created_time','>=',$startTime)
                //->where('qo.created_time','<',$endTime)
                ->sum('p.price'); //房间消费金额
            $info['integral'] = Db::name('qt_viper_card')
                ->where('store_id', '=', $user_info['store_id'])
                ->where('mobile', '=', $user_info['phone'])->sum('score');
            $this->result('获取成功', $info, 200);
        } else {
            $this->result('无效的请求', '', 203);
        }
    }


    /**
     *  点评技师
     **/
    public function addYelp()
    {
        $user_id        = $this->user_id;
        $engineer_id    = $this->request->post('engineer_id');          //技师ID
        $content        = $this->request->post('content', '');   //内容
        $tags           = $this->request->post('tags', '');      //标签
        if (empty($content)) {
            $this->result('请输入内容', '', 204);
        }

        $UsersYelpM     = new UsersYelpM();
        $result         = $UsersYelpM->addYelp($user_id, $engineer_id, $content, $tags);

        if ($result) {
            $this->result('点评成功', '', 200);
        } else {
            $this->result('点评失败', '', 204);
        }
    }

    /**
     * 获取技师点评列表
     */
    public function getYelpList () {

        $user_id        = $this->user_id;
        $engineer_id    = $this->request->post('engineer_id');          //技师ID
        $limit          = $this->request->post('limit', 10);
        $UsersYelpM     = new UsersYelpM();
        $result         = $UsersYelpM->getYelpList($engineer_id,$limit);
        $this->result('点评成功', $result, 200);
    }


    /**
     *  我的团队
     **/
    public function myTeam()
    {
        $user_id        = $this->user_id;
        $store_id       = $this->store_id;
        $limit          = $this->request->post('limit', 10);

        $ViperUserM = new ViperUserM();
        $list       = $ViperUserM->viperTeam($store_id,$user_id, $limit);

        $this->result('获取成功', $list, 200);
    }


    /**
     *  资金记录
     **/
    public function getMoneyLog()
    {
        $user_id        = $this->user_id;
        $store_id       = $this->store_id;
        $flag           = $this->request->post('flag', 1);
        $limit          = $this->request->post('limit', 10);
        $check          = $this->request->post('check', 1);

        $ViperUserM = new ViperUserM();
        $list       = $ViperUserM->viperMoneyLog($user_id, $flag, $check, $limit);

        $this->result('获取成功', $list, 200);
    }

    /**
     *  推广海报
     **/
    public function getPoster()
    {
        $user_id = $this->user_id;
        $info    = ViperUserM::where('id', $user_id)->find();

        $Wx = new Wx(config('wx_mini_appid'), config('wx_mini_secret'));
        $accessToken = $Wx->getWxAccessToken();

        $param['nickname']  = $info['nickname'];
        $param['avatar']    = checkAvatar($info['avatar']);
        $param['ivt_code']  = $info['ivt_code'];
        $param['show']      = config('share_title');  //分享提醒配置(后台未添加)

        list($code, $poster) = collection_poster($accessToken, $user_id, $param, true);
        $poster = getUrl() . '/' . $poster;

        $row['poster'] = $poster;
        $row['yqm']    = $info['ivt_code'];
        $this->result('获取成功', $row, 200);
    }

    /**
     * 获取项目退款记录
     */
    public function getRefundLog()
    {

        $user_id   = $this->user_id;
        $store_id  = $this->store_id;
        $VipOrderM = new VipOrderM();
        $data      = $VipOrderM->getRefundLog($user_id, $store_id);

        $this->result('获取成功', $data, 200);
    }


    /**
     * 获取今日项目列表
     */
    public function getTodayProject()
    {

        if ($this->request->isPost()) {
            $user_id   = $this->user_id;
            $store_id  = $this->store_id;
            $post = $this->request->post();
            $limit = !empty($post['limit']) ? $post['limit'] : 10;

            $startTime = strtotime(date('Y-m-d', time()));
            $endTime = strtotime('+1 day', $startTime);


            $data['list'] = Db::name('qt_order_project')->alias('p')
                ->field([
                    'p.id',
                    'p.name',
                    'p.engineer_id',
                    'p.room_id',
                    'p.num',
                    'p.price',
                    'u.real_name',
                    'u.work_number',
                    'm.name as room_name',
                    'qo.uv_id',
                    'qo.store_id',
                    'qo.created_time',
                ])
                ->leftJoin('qt_users u', 'p.engineer_id=u.id')
                ->leftJoin('qt_order qo', 'p.order_id=qo.id')
                ->leftJoin('qt_room m', 'p.room_id=m.id')
                ->where('qo.uv_id', $user_id)
                ->where('qo.store_id', $store_id)
                ->where('p.is_delete',2)
                //->where('qo.created_time','>=',$startTime)
                //->where('qo.created_time','<',$endTime)
                ->paginate($limit);
            $data['total_price'] =  Db::name('qt_order_project')->alias('p')
                ->field([
                    'p.price',
                ])
                ->leftJoin('qt_users u', 'p.engineer_id=u.id')
                ->leftJoin('qt_order qo', 'p.order_id=qo.id')
                ->leftJoin('qt_room m', 'p.room_id=m.id')
                ->where('qo.uv_id', $user_id)
                ->where('qo.store_id', $store_id)
                ->where('p.is_delete',2)
                //->where('qo.created_time','>=',$startTime)
                //->where('qo.created_time','<',$endTime)
                ->sum('p.price');
            $this->result('查询成功', $data, 200);
        } else {
            $this->result('无效的请求', '', 204);
        }
    }


    /**
     * 获取我的邀请码
     */
    public function getInvitationCode() {

        $url      = config('site_url').'/store/#/'.'?ivt_code='. $this->user_info['ivt_code'];
        $path     = 'uploads/invitation_code/';
        $filename = 'ivt_code_'.$this->user_info['ivt_code'].'.png';
        $result = create_ewm($url,$path,$filename,6);
        $img_path = getUrl().'/'.$result;
        $data['ivt_code'] =  $this->user_info['ivt_code'];
        $data['img_path'] = $img_path;
        $this->result('查询成功', $data, 200);
    }


    /**
     * 获取充值记录
     */
    public function getRechargeLog () {
        if ($this->request->isPost()) {
            $user_id = $this->user_id;
            $store_id = $this->store_id;
            $post = $this->request->post();
            $limit = !empty($post['limit']) ? $post['limit'] : 10;


            $data = Db::name('qt_viper_card_recharge_log')
                ->alias('rl')
                ->field([
                    'rl.id',
                    'rl.amount',
                    'rl.give_amount',
                    'rl.pay_status',
                    'rl.pay_time',
                    'rl.create_time',
                    'rl.pay_type',
                    's.name as rome_name',
                ])
                ->where([
                    'rl.user_id'=>$user_id,
                    'rl.store_id'=>$store_id,
                    'rl.is_revoke'=>2,//没有撤销的充值记录
                    'rl.pay_status'=>1,//已支付的
                ])
                ->leftJoin('qt_store s','s.id=rl.store_id')->order('rl.id desc')
                ->paginate($limit)
                ->each(function ($item){

                    $item['create_time'] = empty($item['create_time']) ? '' : date('Y-m-d H:i:s',$item['create_time']);
                    $item['pay_time'] = empty($item['pay_time']) ? '' : date('Y-m-d H:i:s',$item['pay_time']);
                    return $item;
                });
            $this->result('查询成功', $data, 200);
        } else {
            $this->result('无效的请求', '', 204);
        }
    }
}
